#include "ShowFindUpdateMSH.h"                                                                                                              /** MSH on S1 **/
#include <algorithm>                                                                                                                        /** MSH on S1 **/
#include <iostream>                                                                                                                         /** MSH on S1 **/

using namespace std;                                                                                                                        /** MSH on S1 **/

/**
 * list all categories sorted by category ID
 * print name and number of products in each
 * MSH on S1
 **/
void ShowFindUpdateMSH::show (vector < Category > cats, vector < Product > prds) {                                                          /** MSH on S1 **/
    sort(cats.begin(), cats.end(), ShowFindUpdateMSH::cmpCat);                                                                              /** MSH on S1 **/

    for (vector < Category > ::iterator catIter = cats.begin(); catIter != cats.end(); ++catIter) {                                         /** MSH on S1 **/
        int nPrd = 0;                                                                                                                       /** MSH on S1 **/

        for (vector < Product > ::iterator prdIter = prds.begin(); prdIter != prds.end(); ++prdIter) {                                      /** MSH on S1 **/
            if ((*prdIter).get_cat_id() == (*catIter).get_cat_id()) {                                                                       /** MSH on S1 **/
                ++nPrd;                                                                                                                     /** MSH on S1 **/
            }                                                                                                                               /** MSH on S1 **/
        }                                                                                                                                   /** MSH on S1 **/
        cout << (*catIter).get_cat_name() << " has " << nPrd << " product(s)" << endl;                                                      /** MSH on S1 **/
    }                                                                                                                                       /** MSH on S1 **/
}                                                                                                                                           /** MSH on S1 **/

/**
 * display total sales for a given product
 * MSH on S1
 **/
void ShowFindUpdateMSH::find (int prdID, vector < Product > prds, vector < Cart_item > cartItems) {                                         /** MSH on S1 **/
    bool prdGate = false;                                                                                                                   /** MSH on S1 **/
    for (vector < Product > ::iterator prdIter = prds.begin(); ((prdIter != prds.end()) && ! prdGate); ++prdIter) {                         /** MSH on S1 **/
        if ((*prdIter).get_prod_id() == prdID) {                                                                                            /** MSH on S1 **/
            ShowFindUpdateMSH::find((*prdIter), cartItems);                                                                                 /** MSH on S1 **/
            prdGate = true;                                                                                                                 /** MSH on S1 **/
        }                                                                                                                                   /** MSH on S1 **/
    }                                                                                                                                       /** MSH on S1 **/
}                                                                                                                                           /** MSH on S1 **/

/**
 * add a new product to an existing category
 * MSH on S1
 **/
void ShowFindUpdateMSH::update (int prdID, int catID, string prdName, double prdPrice, vector < Category > cats, vector < Product > &prds) {/** MSH on S1 **/

#ifdef DEBUG                                                                                                                                /** MSH on S1 **/
    for (vector < Category > ::iterator catIter = cats.begin(); catIter != cats.end(); ++catIter) {                                         /** MSH on S1 **/
        if ((*catIter).get_cat_id() == catID) {                                                                                             /** MSH on S1 **/
            int nPrd = 0;                                                                                                                   /** MSH on S1 **/

            for (vector < Product > ::iterator prdIter = prds.begin(); prdIter != prds.end(); ++prdIter) {                                  /** MSH on S1 **/
                if ((*prdIter).get_cat_id() == (*catIter).get_cat_id()) {                                                                   /** MSH on S1 **/
                    ++nPrd;                                                                                                                 /** MSH on S1 **/
                }                                                                                                                           /** MSH on S1 **/
            }                                                                                                                               /** MSH on S1 **/
            cout << (*catIter).get_cat_id() << "has " << nPrd << " product(s)" << endl;                                                     /** MSH on S1 **/
        }                                                                                                                                   /** MSH on S1 **/
    }                                                                                                                                       /** MSH on S1 **/
#endif                                                                                                                                      /** MSH on S1 **/

    bool catIDExists = false, prdIDExists = false;                                                                                          /** MSH on S1 **/

    for (vector < Category > ::iterator catIter = cats.begin(); ((catIter != cats.end()) && ! catIDExists); ++catIter) {                    /** MSH on S1 **/
        if ((*catIter).get_cat_id() == catID) {                                                                                             /** MSH on S1 **/
            catIDExists = true;                                                                                                             /** MSH on S1 **/
        }                                                                                                                                   /** MSH on S1 **/
    }                                                                                                                                       /** MSH on S1 **/

    if (catIDExists) {                                                                                                                      /** MSH on S1 **/
        for (vector < Product > ::iterator prdIter = prds.begin(); ((prdIter != prds.end()) && ! prdIDExists); ++prdIter) {                 /** MSH on S1 **/
            if ((*prdIter).get_prod_id() == prdID) {                                                                                        /** MSH on S1 **/
                prdIDExists = true;                                                                                                         /** MSH on S1 **/
            }                                                                                                                               /** MSH on S1 **/
        }                                                                                                                                   /** MSH on S1 **/

        if (! prdIDExists) {
			if (prdPrice > 0){		
				Product prd = Product(prdID, catID, prdName, prdPrice);                                                                         /** MSH on S1 **/
				prds.push_back(prd);                                                                                                            /** MSH on S1 **/

				cout << "product added" << endl;
			}																																
			else{																															
				cout << "Product price must be greater than zero" << endl;																	
			}																																
        } else {                                                                                                                            /** MSH on S1 **/
            cout << "product with ID " << prdID << " exists already" << endl;                                                               /** MSH on S1 **/
        }                                                                                                                                   /** MSH on S1 **/
    } else {                                                                                                                                /** MSH on S1 **/
        cout << "category doesn't exist" << endl;                                                                                           /** MSH on S1 **/
    }                                                                                                                                       /** MSH on S1 **/

#ifdef DEBUG                                                                                                                                /** MSH on S1 **/
    for (vector < Category > ::iterator catIter = cats.begin(); catIter != cats.end(); ++catIter) {                                         /** MSH on S1 **/
        if ((*catIter).get_cat_id() == catID) {                                                                                             /** MSH on S1 **/
            int nPrd = 0;                                                                                                                   /** MSH on S1 **/

            for (vector < Product > ::iterator prdIter = prds.begin(); prdIter != prds.end(); ++prdIter) {                                  /** MSH on S1 **/
                if ((*prdIter).get_cat_id() == (*catIter).get_cat_id()) {                                                                   /** MSH on S1 **/
                    ++nPrd;                                                                                                                 /** MSH on S1 **/
                }                                                                                                                           /** MSH on S1 **/
            }                                                                                                                               /** MSH on S1 **/
            cout << (*catIter).get_cat_id() << "has " << nPrd << " product(s)" << endl;                                                     /** MSH on S1 **/
        }                                                                                                                                   /** MSH on S1 **/
    }                                                                                                                                       /** MSH on S1 **/
#endif                                                                                                                                      /** MSH on S1 **/

}                                                                                                                                           /** MSH on S1 **/

/**
 * comparison function that taking two values of Category
 * returns true if and only if the first argument goes before the second argument in the spesific strict weak ordering defines and false otherwise
 * a Category is before another Category if and only if the former's category ID is before the last's
 * MSH on S1
 **/
bool ShowFindUpdateMSH::cmpCat (Category cat1, Category cat2) {                                                                             /** MSH on S1 **/
    return (cat1.get_cat_id() < cat2.get_cat_id());                                                                                         /** MSH on S1 **/
}                                                                                                                                           /** MSH on S1 **/

/**
 * display total sales for a given product
 * MSH on S1
 **/
void ShowFindUpdateMSH::find (Product prd, vector < Cart_item > cartItems) {                                                                /** MSH on S1 **/
    int prdID = prd.get_prod_id();                                                                                                          /** MSH on S1 **/
    double prdPrice = prd.get_prod_price();                                                                                                 /** MSH on S1 **/
    double total = 0;                                                                                                                       /** MSH on S1 **/

    for (vector < Cart_item > ::iterator cartItemIter = cartItems.begin(); cartItemIter != cartItems.end(); ++cartItemIter) {               /** MSH on S1 **/
        if ((*cartItemIter).get_prod_id() == prdID) {                                                                                       /** MSH on S1 **/
            total += (*cartItemIter).get_prod_qty() * prdPrice;                                                                             /** MSH on S1 **/
        }                                                                                                                                   /** MSH on S1 **/
    }                                                                                                                                       /** MSH on S1 **/
    cout << "total sales for product " << prd.get_prod_name() << " is " << total << endl;                                                   /** MSH on S1 **/
}                                                                                                                                           /** MSH on S1 **/
